import type {BlogUserWebVO} from "~/api/user/types";

const TOKEN_KEY = "Blog-Token";
const USER_KEY = "Blog-User";
const MAX_AGE = 86400;
export const setToken = (token: string) => {
    useCookie(TOKEN_KEY, {
        maxAge: MAX_AGE
    }).value = token;
};

export const getToken = () => {
    return useCookie(TOKEN_KEY)?.value;
};

export const removeToken = () => {
    useCookie(TOKEN_KEY).value = undefined;
};

export const removeUserInfo = () => {
    useCookie(USER_KEY).value = undefined;
};

// 添加用户信息到cookie
export const setUserInfoToCookie = async (userInfo: BlogUserWebVO) => {
    useCookie(USER_KEY, {
        maxAge: MAX_AGE
    }).value = JSON.stringify(userInfo)
};

// 获取用户信息
export const getUserInfoFromCookie = async () => {
    return useCookie(USER_KEY).value as unknown as BlogUserWebVO;
};
